<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">考卷管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>考卷管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            考卷名：
            <input id="exam-paper-name" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            参考小组：
            <select id="exam-paper-group">
                <option value="">-请选择-</option>
            </select>&emsp;
            <button id="exam-paper-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="exam-paper-btn-add" class="layui-btn icon-btn">发布考试</button>
        </div>
    </div>
    <div class="layui-card-body">
        <table class="layui-table" id="exam-paper" lay-filter="exam-paper-filter"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="exam-paper-table-bar" >
    <a class="layui-btn layui-btn-primary  layui-btn-xs "  lay-event="paper-look">查看</a>
    <a class="layui-btn layui-btn-primary  layui-btn-xs "  lay-event="paper-question-add">添加考题</a>
    <a class="layui-btn layui-btn-danger  layui-btn-xs "  lay-event="paper-question-del">删除</a>
</script>


<script>
    layui.use(['index','form', 'table', 'util', 'config', 'admin','layer'], function () {
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var index = layui.index;
        var layer = layui.layer;
        var form = layui.form;
        let creator;
        index.getUser(function (user) {
            creator=user.id
        });
        //获取小组
        let param={
            teacid:creator,
            access_token:config.getToken().access_token
        };
        $.ajax({
            type: 'post',
            url: config.base_server+'api-user/group/getGroupByTeacher',
            data: param,
            async: false, //同步
            dataType: 'json',
            xhrFields: {
                withCredentials: true //允许跨域带Cookie
            },
            success: function(res) {
                let data=res.data;
                for (let i = 0; i < data.length; i++) {
                    $("#exam-paper-group").append('<option value="'+data[i].grouid+'">'+data[i].gpname+'</option>')
                }
                form.render();
            }
        });
        // 渲染表格
        table.render({
            elem: '#exam-paper',
            url: config.base_server + 'api-exam/examPaper/findPaper',
            method: 'get',
            where: {
                access_token: config.getToken().access_token,
                creator:creator
            },
            page: true,
            height:'full-350',
            cols: [[
                {type: 'numbers' },
                {field: 'epname', title: '试卷名'},
                {field: 'remark', title: '简介要求'},
                {field: 'gpname', title: '参考小组'},
                {field: 'status', title: '参考情况',
                    templet:function (data) {
                        if(data.status==2){
                            return "有人参考"
                        }else {
                            return ""
                        }
                    }
                },
                {field: 'nickName',width: 120, title: '创建人'},
                {field: 'cretim',width: 120, title: '创建时间'},
                {align: 'center', toolbar: '#exam-paper-table-bar', title: '操作', width: 250}
            ]],
            limit:10,
        });

        table.on('tool(exam-paper-filter)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'paper-question-add') { // 添加考题
                paperQuestionAdd(data)
            }
            if (layEvent === 'paper-look') { // 查看详情
                paperLook(data)
            }
            if (layEvent === 'paper-question-del') { // 删除试卷
                if(data.status==2){
                    layer.msg("已有人参考，不能删除");
                    return false;
                }
                admin.req("api-exam/examPaper/removePaper/"+data.expaid,{},function (res) {
                    layer.msg(res.msg);
                    if(res.code===0){
                        table.reload("exam-paper")
                    }
                },"post");
            }
        });
        //添加考题
        function paperQuestionAdd(data){
            admin.req("api-exam/examPaper/findPaperQue",{expaid:data.expaid},function (res) {
                if(res.data.length>0){
                    layer.msg("不能修改试卷考题")
                    return false;
                }else {
                    admin.putTempData("paper_data",data);
                    admin.popupCenter({
                        title: '添加考题：'+data.epname,
                        area: ['50%','75%'],
                        path: 'pages/exam/paper-question-add.html',
                        success:function(layero){
                            $(layero).children('.layui-layer-content').css('overflow-y', 'auto');
                        }
                    });
                }
            },"get");
        }
        //查看试卷
        function paperLook(data){
            admin.req("api-exam/examPaper/findPaperQue",{expaid:data.expaid},function (res) {
                if(res.data.length<=0){
                    layer.msg("没有题目！");
                    return false;
                }else {
                    admin.putTempData("paper_data",data);
                    admin.popupCenter({
                        title: '试卷：'+data.epname,
                        area: ['50%','75%'],
                        path: 'pages/exam/paper-question-look.html',
                        success:function(layero){
                            $(layero).children('.layui-layer-content').css('overflow-y', 'auto');
                        },
                        finish: function () {
                            table.reload('exam-paper');
                        }
                    });
                }
            },"get");
        }
        //条件搜索
        $("#exam-paper-btn-search").click(function () {
            let epname = $("#exam-paper-name").val();
            let grouid = $("#exam-paper-group").val();
            let param={epname:epname,grouid:grouid};
            table.reload('exam-paper', {where: param});
        });
        //添加试卷
        $("#exam-paper-btn-add").click(function () {
            admin.popupCenter({
                title: '考卷新增',
                area: ['40%','75%'],
                path: 'pages/exam/exam-paper-add.html',
                success:function(layero){
                    $(layero).children('.layui-layer-content').css('overflow-y', 'auto');
                },
                finish: function () {
                    table.reload('exam-paper');
                }
            });
        });

    });
</script>